package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;
import com.wwwc.util.web.*;

public class ServletBean {

    public static StringBuffer getXmlComments(String title) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("\n<!-- ============================= "+title+" ============================= -->\n");
	return sbf;
    }

    public static StringBuffer getStyleSheet(String title) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n");
	sbf.append("<head>\n");
	sbf.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n");
	sbf.append("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n");
	sbf.append("<title>\""+title+"\"</title>\n");
	sbf.append("</head>\n");
	sbf.append("<link rel=\"shortcut icon\" href=\"/images/favicon.ico\" type=\"image/x-icon\" />\n");
	return sbf;
    }

    public static StringBuffer getStyleSheet(String title, String key_words) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n");
	sbf.append("<head>\n");
	sbf.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n");
	sbf.append("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n");
	if(key_words != null && key_words.length()>0) {
	    sbf.append("<meta name=\"keywords\" content=\""+key_words+"\" />\n");
	}
	sbf.append("<title>\""+title+"\"</title>\n");
	sbf.append("</head>\n");
	sbf.append("<link rel=\"shortcut icon\" href=\"/images/favicon.ico\" type=\"image/x-icon\" />\n");
	return sbf;
    }

    public static StringBuffer getStyleSheet(String title, String key_words, String refresh) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n");
	sbf.append("<head>\n");
	sbf.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n");
	sbf.append("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n");
	if(refresh != null && refresh.length()>0) {
	    sbf.append("<meta http-equiv=\"Refresh\" Content=\""+refresh+"\">");
	}
	if(key_words != null && key_words.length()>0) {
	    sbf.append("<meta name=\"keywords\" content=\""+key_words+"\">\n");
	}
	sbf.append("<title>\""+title+"\"</title>\n");
	sbf.append("</head>\n");
	sbf.append("<link rel=\"shortcut icon\" href=\"/images/favicon.ico\" type=\"image/x-icon\" />\n");
	return sbf;
    }

    public StringBuffer getSubdirInfo (HttpServletRequest request)
    {
	StringBuffer sbf = new StringBuffer();
	HttpSession session  = request.getSession(true);
	Hashtable dir_hs = (Hashtable)session.getAttribute("ContralOpenClose");
	String call_path = request.getContextPath()+request.getServletPath();

	if(dir_hs != null) {
	    String  mid     =  (String)request.getAttribute("mid");
	    String managers =  null;
	    if(mid != null) {
		managers = (String)dir_hs.get(mid+"_managers");
	    }

	    String sub_dir =  request.getParameter("sub_dir");
	    
	    if(managers == null || managers.length()<1) {
		managers = "<A href='"+call_path+"?maid=amg' style='text-decoration: none'>Apply</A>";
	    }

	    sbf.append("\n<TABLE cellSpacing='0' cellPadding='2' border='0' ");
	    sbf.append(" width=100% style='border-bottom:1px solid'>");
	    sbf.append("\n<TR>");
	    sbf.append("\n<TD align=center>");
	    sbf.append("<B>"+sub_dir+"</B></TD>");
	    sbf.append("\n<TD align=center>");
	    sbf.append("Manager(s):&nbsp;["+managers+"]</TD>");
	    sbf.append("\n<TD align=center>");
	    sbf.append("Access Level:&nbsp;[<B>"+dir_hs.get(mid+"_read_level")+"</B>]</TD>");
	    sbf.append("\n<TD align=center>");
	    sbf.append("Access Age:&nbsp;[<B>"+dir_hs.get(mid+"_read_age")+"</B>]</TD>");
	    sbf.append("\n</TR>");
	    sbf.append("\n</TABLE>");
	}
	return sbf;
    }
    /*
    public static boolean isManager(HttpServletRequest request, String username, String dim) {
	HttpSession session  = request.getSession(true);
	Hashtable dir_hs = (Hashtable)session.getAttribute("ContralOpenClose");
	if(dir_hs != null) {
	    String id = (String)request.getAttribute("id");
	    if(id == null) {
		return false;
	    } 
	    String managers = (String)dir_hs.get(id+"_manager");
	    if(username == null || username.length()<1 || managers == null || managers.length()<1) {
		return false;
	    }

	    if(managers.indexOf(dim) == -1) {
		return(username.equalsIgnoreCase(managers));
	    }
	    else {
		StringTokenizer stk = new StringTokenizer(managers, dim);
		while(stk.hasMoreTokens()) {
		    if(username.equalsIgnoreCase(stk.nextToken())) {
			return true;
		    }
		}
	    }
	}
	return false;
    }
    */
    
    public static boolean isManager(String managers, String username, String dim) {
	if(username == null || username.length()<1 || managers == null || managers.length()<1) {
	    return false;
	}

	if(managers.indexOf(dim) == -1) {
	    return(username.equalsIgnoreCase(managers));
	}
	else {
	    StringTokenizer stk = new StringTokenizer(managers, dim);
	    while(stk.hasMoreTokens()) {
		if(username.equalsIgnoreCase(stk.nextToken())) {
		    return true;
		}
	    }
	}
	return false;
    }
    
    public static StringBuffer resetUrl (String url) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<HTML><HEAD>");
	sbf.append("<TITLE>user sign in</TITLE>");
	sbf.append("</HEAD>");
	sbf.append("<SCRIPT>");
	sbf.append("function DoLogIn() {");
	sbf.append("  top.location.replace(\""+url+"\");");
	sbf.append("  return true;");
	sbf.append("}");
	sbf.append("</SCRIPT>");
	sbf.append("<BODY onLoad=DoLogIn();>");
	sbf.append("</BODY></HTML>");
	return sbf;
    }
}






